<%--
Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
--%>

<%@ Control Language="C#" ClassName="GrurrahEditControl" Inherits="Grr.Web.GrurrahControl" %>
<%@ Register Src="WYSIWYGControl.ascx" TagName="WYSIWYGControl" TagPrefix="uc1" %>
<%@ Register Assembly="Gaia.WebWidgets" Namespace="Gaia.WebWidgets" TagPrefix="gaia" %>

<%@ Import Namespace="Grr.Model" %>
<%@ Import Namespace="Grr.Web" %>

<script runat="server">

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (!IsPostBack)
            UpdateGrurrahElements();
    }

    protected override bool OnBubbleEvent(object source, EventArgs args)
    {
        WYSIWYGCommandEventArgs we = args as WYSIWYGCommandEventArgs;

        if (we != null)
        {
            if (we.Command == WYSIWYGCommand.Cancel)
                Cancel();
            else
                Save();
        }
        return base.OnBubbleEvent(source, args);
    }
    
    public override void UpdateGrurrahElements()
    {
        Grurrah g = (Grurrah)Session["CurrentGrurrah"];

        if (g != null)
        {
            Title.Text = g.Name;
            WYSIWYGControl.Text = g.DescriptionBody;
        }
    }

    private void Save ()
    {
        string text = WYSIWYGControl.Text;

        Grurrah g = (Grurrah)Session["CurrentGrurrah"];
        GrurrahUser gu = (GrurrahUser)Session["CurrentGrurrahUser"];
        string user_id = String.Empty;

        if (gu != null)
            user_id = gu.Name;
       
        if (g != null)
            g.UpdateDescriptionText(text, user_id, Request.UserHostAddress);

        RaiseBubbleEvent(this, new GrurrahPageModeChangedEventArgs(GrurrahPageMode.Read));
    }

    private void Cancel ()
    {
        RaiseBubbleEvent(this, new GrurrahPageModeChangedEventArgs(GrurrahPageMode.Read));
    }
</script>

<table style="margin:0px 15px 15px 15px;">
        <tr>
            <td>
                <h1><gaia:Label ID="Title" runat="server"></gaia:Label></h1></td>
        </tr>
        <tr>
            <td style="vertical-align:top;" valign="top">
                <uc1:WYSIWYGControl ID="WYSIWYGControl" runat="server" />
            </td>
        </tr>
    </table>
